#include <iostream>
#include "INET_Addr.h"
#include "SOCK_Acceptor.h"
#include "SOCK_Stream.h"

using namespace std;

#define _port 3492
#define _len 50

void main()
{
	cout << "SERVER with ip: ";

	char helloMessage[_len] = "Hello client\r\n";
	char recvBuf[_len];
	bool start = false;

	while(true)
	{
		SOCK_Stream stream = SOCK_Stream();
		INET_Addr address = INET_Addr(_port, INADDR_ANY);
		SOCK_Acceptor acceptor = SOCK_Acceptor();
		if(!start)
			cout << acceptor.myIp() << endl << endl;
		
		start = true;
	
		if(true == acceptor.open(address))
		{
			cout << "Waiting for new client..." << endl;
			acceptor.accept(stream);
		}
	
		else
			cout << "Error has occured. Could not open connection." << endl;
	
		recvBuf[stream.recv(&recvBuf,_len,0)]='\0';
		cout << "Connection established to: " << recvBuf << endl << "Connection closed." << endl << endl;
	}
}